package artid.com.arttogo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
 *
 *
 * 项目名称：ArtworkArt   
 * 类名称：HeaderViewListAdapter   
 * 类描述：用于瀑布流图片上圆下方图片和图片高度设置
 * 创建人：耿卫斌   
 * 创建时间：2017-1-4 上午10:51:56   
 *
 */
public class DynamicHeightImageView extends ImageView {
    public Boolean isRightAngle=false;//是否为直角(默认是圆角)
    private float[] rids = {15.0f,15.0f,15.0f,15.0f,0.0f,0.0f,0.0f,0.0f,};
    private double mHeightRatio;
    private int height;
    public DynamicHeightImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DynamicHeightImageView(Context context) {
        super(context);
    }

    public void setHeightRatio(double ratio) {
        if (ratio != mHeightRatio) {
            mHeightRatio = ratio;
            requestLayout();
        }
    }

    public double getHeightRatio() {
        return mHeightRatio;
    }

    public void setHeight(int height){
        this.height=height;
        requestLayout();
    }

    /**
     * ��ͼ
     * by Hankkin at:2015-08-30 21:15:53
     * @param canvas
     */
    protected void onDraw(Canvas canvas) {
        if(!isRightAngle) {
            Path path = new Path();
            int w = this.getWidth();
            int h = this.getHeight();
            path.addRoundRect(new RectF(0, 0, w, h), rids, Path.Direction.CW);
            canvas.clipPath(path);
        }
        super.onDraw(canvas);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (height > 0.0) {
            // set the image views size
            int width = MeasureSpec.getSize(widthMeasureSpec);
//			int height = (int) (width * mHeightRatio);
            setMeasuredDimension(width, height);
        }
        else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
}
